package com.xmall.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmall.define.dict.IsDefaultEnum;
import com.xmall.product.define.dto.spu.SpuImageDto;
import com.xmall.product.define.request.spu.SpuImageRequest;
import com.xmall.product.entity.SpuImage;
import com.xmall.product.mapper.SpuImageMapper;
import com.xmall.product.service.ISpuImageService;
import com.xmall.product.utils.ConvertUtils;
import com.xmall.utils.XmallUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 *  服务实现类
 */
@Service
public class SpuImageServiceImpl extends ServiceImpl<SpuImageMapper, SpuImage> implements ISpuImageService {

    /**
     * 保存
     */
    @Override
    public void save(Long spuId, List<SpuImageRequest> images) {
        images.forEach(image -> image.setSpuId(spuId));
        if(CollectionUtil.isNotEmpty(images)){
            this.saveBatch(ConvertUtils.SpuImageModule.INSTANCE.toEntityList(images));
        }
    }

    @Transactional
    @Override
    public void update(Long spuId, List<SpuImageRequest> images) {
        this.remove(new QueryWrapper<SpuImage>().eq("spu_id", spuId));
        this.save(spuId, images);
    }

    /**
     * 删除
     */
    @Override
    public void delete(Long spuId) {
        this.remove(new QueryWrapper<SpuImage>().eq("spu_id", spuId));
    }

    /**
     * spu图片列表
     */
    @Override
    public List<SpuImageDto> list(Long spuId) {
        List<SpuImage> spuImageList = this.list(new QueryWrapper<SpuImage>().eq("spu_id", spuId));
        return ConvertUtils.SpuImageModule.INSTANCE.toDtoList(spuImageList);
    }

    /**
     * spu图片列表
     */
    @Override
    public List<String> imageList(Long spuId) {
        return this.list(spuId).stream()
            .sorted(Comparator.comparing(SpuImageDto::getSort))
            .map(spuImage -> spuImage.getUrl())
            .filter(url -> StrUtil.isNotBlank(url))
            .collect(Collectors.toList());
    }

    /**
     * 获取默认图片
     */
    @Override
    public String getDefaultImage(List<SpuImageRequest> images) {
        XmallUtils.notEmpty(images);
        Optional<SpuImageRequest> defaultImage = images.stream().filter(image -> IsDefaultEnum.YES.getValue() == image.getIsDefault()).findAny();
        if(!defaultImage.isPresent()) {
            images.get(0).setIsDefault(IsDefaultEnum.YES.getValue());
            return images.get(0).getUrl();
        }else {
            return defaultImage.get().getUrl();
        }
    }

    /**
     * 获取默认图片
     */
    @Override
    public Long getDefaultImageSn(List<SpuImageDto> images) {
        Optional<SpuImageDto> defaultSpuImage = images.stream().filter(spuImage -> IsDefaultEnum.YES.getValue() == spuImage.getIsDefault()).findAny();
        return defaultSpuImage.isPresent() ? defaultSpuImage.get().getSn() : null;
    }

}
